#!/usr/bin/env Rscript

library(tidyverse)
library(lubridate)
library(padr)

complete <- read_csv2("../app/data/completo.csv", na = "NA",
                      col_types = cols(
                          pais = col_character(),
                          regiao = col_character(),
                          uf = col_character(),
                          mesorregiao = col_character(),
                          microrregiao = col_character(),
                          municipio = col_character(),
                          st_acidente_feriado = col_character(),
                          ds_agente_causador = col_character(),
                          ano_cat = col_integer(),
                          ds_cnae_classe_cat = col_character(),
                          dt_acidente = col_date(),
                          st_dia_semana_acidente = col_character(),
                          ds_emitente_cat = col_character(),
                          hora_acidente = col_time(),
                          idade_cat = col_integer(),
                          cd_indica_obito = col_character(),
                          ds_natureza_lesao = col_character(),
                          ds_cbo = col_character(),
                          ds_parte_corpo_atingida = col_character(),
                          cd_tipo_sexo_empregado_cat = col_character(),
                          ds_tipo_acidente = col_character(),
                          ds_tipo_local_acidente = col_character()
                      ))
   
c <- complete %>%
    filter(ano_cat == 2012) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia.csv_2012", delim = ";")

# 2013
c <- complete %>%
    filter(ano_cat == 2013) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia_2013.csv", delim = ";")

#2014
c <- complete %>%
    filter(ano_cat == 2014) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia_2014.csv", delim = ";")

#2015
c <- complete %>%
    filter(ano_cat == 2015) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia_2015.csv", delim = ";")

#2016
c <- complete %>%
    filter(ano_cat == 2016) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia_2016.csv", delim = ";")

#2017
c <- complete %>%
    filter(ano_cat == 2017) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2017-01-01"),
        end_val = ymd("2017-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes)

c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/br_mes_dia_2017.csv", delim = ";")

# Filter: By all states of Brazil and by year
# 2012
c <- complete %>%
    filter(ano_cat == 2012) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2012.csv", delim = ";")

# 2013
c <- complete %>%
    filter(ano_cat == 2013) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2013.csv", delim = ";")

# 2014
c <- complete %>%
    filter(ano_cat == 2014) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2014.csv", delim = ";")

# 2015
c <- complete %>%
    filter(ano_cat == 2015) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2015.csv", delim = ";")

# 2016
c <- complete %>%
    filter(ano_cat == 2016) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2016.csv", delim = ";")

# 2017
c <- complete %>%
    filter(ano_cat == 2017) %>%
    select(uf, dt_acidente) %>%
    group_by(uf, dt_acidente) %>%
    summarize(acidentes = n()) %>%
    pad(start_val = ymd("2017-01-01"),
        end_val = ymd("2017-12-31"),
        break_above = 2) %>%
    fill_by_value(acidentes, 0) %>%
    mutate(dia = day(dt_acidente),  mes = month(dt_acidente)) %>%
    select(uf, mes, dia, acidentes) %>%
    group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))

write_delim(c, "../app/data/heatmap/estados_mes_dia_2017.csv", delim = ";")
